package com.spyz.P8_OutputFormat_1;

import org.apache.commons.io.output.NullWriter;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

public class LogRecordWriter extends RecordWriter<Text, NullWriter> {

    private  FSDataOutputStream myURL;
    private  FSDataOutputStream otherURL;

    public LogRecordWriter(TaskAttemptContext job) {
        // 准备创建两条流
        try {

            FileSystem fs = FileSystem.get(job.getConfiguration());
            myURL = fs.create(new Path("./output/OutputFormat/myURL.log"));

            this.otherURL = fs.create(new Path("./output/OutputFormat/otherURL.log"));
            FSDataOutputStream otherURL = this.otherURL;

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void write(Text text, NullWriter nullWriter) throws IOException, InterruptedException {
        String log = text.toString();
        // 具体写

        if (log.contains("spyz")){
            myURL.writeBytes(log+"\n");
        }else {
            otherURL.writeBytes(log+"\n");
        }

    }

    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        // 关流
        IOUtils.closeStream(myURL);
        IOUtils.closeStream(otherURL);
    }
}
